cmake_minimum_required(VERSION 3.0)
# set(CMAKE_BUILD_TYPE "Debug")
# set(CMAKE_BUILD_TYPE "Release")

# 设置默认构建类型为Release
# if(NOT CMAKE_BUILD_TYPE)
#     set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "选择构建类型: Debug, Release, RelWithDebInfo, MinSizeRel" FORCE)
# endif()
# set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_BUILD_TYPE "Release")
# 定义自定义优化选项
option(ENABLE_AGGRESSIVE_OPTIMIZATION "启用激进优化" ON)
option(ENABLE_SANITIZERS "启用内存检测" OFF)

# 针对不同构建类型设置编译选项
if(CMAKE_BUILD_TYPE STREQUAL "Release")
    # 基础Release优化
    set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O2  -DNDEBUG")
    set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2  -DNDEBUG")
    # set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O2 -g -DNDEBUG")
    # set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2 -g -DNDEBUG")
    # set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O2")
    # set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2")

    # 激进优化（如果启用）
    if(ENABLE_AGGRESSIVE_OPTIMIZATION)
        if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
            set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3 -march=native -ffast-math")
            set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3 -march=native -ffast-math")
        elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
            set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /O2 /GL /arch:AVX2")
            set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /O2 /GL /arch:AVX2")
        endif()
    endif()
    
    # 内存检测（如果启用）
    if(ENABLE_SANITIZERS)
        set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -fsanitize=address -fno-omit-frame-pointer")
        set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -fsanitize=address -fno-omit-frame-pointer")
        set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} -fsanitize=address")
    endif()
elseif(CMAKE_BUILD_TYPE STREQUAL "Debug")
    # Debug模式选项
    set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0 -g -D_DEBUG")
    set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 -g -D_DEBUG")
    # set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0 -g  -DNDEBUG -D_DEBUG")
    # set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 -g   -DNDEBUG -D_DEBUG")
endif()



project(ai_subsys_sdk)

# add_definitions(-DSIMULATION)
# add_definitions(-DDEBUG)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
message(STATUS "main project dir: " ${PROJECT_SOURCE_DIR})
message(STATUS "system processor: ${CMAKE_SYSTEM_PROCESSOR}")

if (CMAKE_CROSSCOMPILING)
	set(THREADS_PTHREAD_ARG
		"PLEASE_FILL_OUT-FAILED_TO_RUN"
		CACHE STRING "Result from TRY_RUN" FORCE)
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
endif()

# 判断是否为rsiv64架构
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64")
	# 只编译部分模块
	add_subdirectory(common)
	add_subdirectory(pt_sdk)
	add_subdirectory(cim_sdk)
	add_subdirectory(build_model)
	add_subdirectory(sample/cim_online_test)
	add_subdirectory(sample/pt_online_test)
	# add_subdirectory(sample/pt_online_demo)
	add_subdirectory(sample/0_model_builder_demo)
	# add_subdirectory(sample/pt_online_demo_v2)
	# add_subdirectory(sample/pt_online_demo_v3)
	# add_subdirectory(sample/pt_online_demo_v4)
	# 其他rsiv64专用模块
else()
	# 默认全部编译
	add_subdirectory(common)
	add_subdirectory(pt_sdk)
	add_subdirectory(lidar_sdk)
	add_subdirectory(build_model)
	add_subdirectory(sample/Conv3D_Demo)
	add_subdirectory(sample/lidar_record)
	add_subdirectory(sample/lidar_config)
	add_subdirectory(sample/pt_offline_test)
	add_subdirectory(sample/pt_online_test)
	# add_subdirectory(sample/pt_online_demo)
	# add_subdirectory(sample/pt_online_demo_v2)
	# add_subdirectory(sample/pt_online_demo_v3)
	# add_subdirectory(sample/optimized_online_model_v1)
	add_subdirectory(display_tools)
	add_subdirectory(sample/0_model_builder_demo)
endif()